import WsBaseClass from './wsBase.js';


class ttsWsClass extends WsBaseClass {
  constructor(url,msg,appkey,voice,apiType,volume = 50 , speech_rate = 0 , pitch_rate = 0)
  {
    super(url);
    this.msg = msg;
    this.appkey = appkey;
    this.voice = voice;
    this.apiType = apiType;
    this.volume = volume;
    this.speech_rate = speech_rate;
    this.pitch_rate = pitch_rate;
    this.phoneme_list = [];
  }
  onMessage(event)
  {
    let _self = this;
    let data = JSON.parse(event.data);
    if(data.status == 200)
    {
      let actionName = data.name;
      let sendData = {};
      switch(actionName)
      {
        case 'connected':
          sendData = {
              "apiType":_self.apiType,
              "name": "start",
              "appkey": _self.appkey,
              "voice": _self.voice,
              "volume":_self.volume,
              "speech_rate" : _self.speech_rate,
              "pitch_rate" :_self.pitch_rate,
          }
          _self.ws.send(JSON.stringify(sendData));
          break;
        case 'started':
          sendData = {
            "name": "runSynthesis",
            "appkey": _self.appkey,
            "text": _self.msg,
          }
          _self.ws.send(JSON.stringify(sendData));
          break;
        case "synthesis":
          // 接收音素数据
          data.subtitles.forEach(subtitle => {
              if(subtitle.phoneme != "null")
              {
                let phoneme_list = subtitle.phoneme_list;
                phoneme_list.forEach(phoneme =>{
                  _self.phoneme_list.push(phoneme);
                });
              }
          });
          break;
        case "completed":
          // console.log(data);
          // console.log(_self.phoneme_list)
          // 播放音频，对应音素口型
          _self.createAudioSuccess(data.audioUrl,_self.msg, _self.phoneme_list);
          _self.ws.close();
        break;
      }

    }
  }
  createAudioSuccess(audioUrl,msg,phoneme_list){}

}


export default ttsWsClass;